// 基础接口定义
interface BaseResource {
    id: string;                 // 基础类型
    name: string;
    createTime: Date;          // 日期类型
    status: 'active' | 'inactive';  // 字面量联合类型
  }
  
  // 接口继承
interface ResourceDetail extends BaseResource {
    description?: string;      // 可选属性，使用 ?
    readonly owner: string;    // 只读属性，使用 readonly
    tags: string[];           // 数组类型
  }
  
  // 泛型接口
  interface ApiResponse<T> {
    code: number;
    data: T;
    message: string;
  }


// 嵌套接口
interface ResourceConfig {
  basic: {
    name: string;
    type: ResourceType;
  };
  spec: {
    cpu: number;
    memory: number;
    gpu?: {
      count: number;
      type: string;
    };
  };
  network: {
    bandwidth: number;
    publicIP?: boolean;
  };
}

// 函数类型接口
interface ResourceService {
  create: (config: ResourceConfig) => Promise<ResourceDetail>;
  delete: (id: ResourceId) => Promise<boolean>;
  update: (id: ResourceId, config: Partial<ResourceConfig>) => Promise<ResourceDetail>;
}

// 类型断言
interface GPUResource extends ResourceDetail {
  gpuType: string;
}

function isGPUResource(resource: ResourceDetail): resource is GPUResource {
  return 'gpuType' in resource;
}

// 使用类型守卫
function handleResource(resource: ResourceDetail) {
  if (isGPUResource(resource)) {
    console.log(resource.gpuType); // TypeScript 知道这里 resource 是 GPUResource 类型
  }
}

// API 请求响应
interface ListResponse<T> {
  total: number;
  items: T[];
  page: number;
  pageSize: number;
}

// 组件 Props
interface ResourceCardProps {
  data: ResourceDetail;
  onDelete?: (id: string) => void;
  style?: React.CSSProperties;
}

// 状态管理
interface ResourceState {
  loading: boolean;
  data: ResourceDetail | null;
  error?: string;
}


// 类型别名
type ResourceId = string;

// 联合类型
type ResourceStatus = 'pending' | 'running' | 'stopped';

// 交叉类型
type ResourceWithQuota = ResourceDetail & {
  quota: {
    cpu: number;
    memory: number;
  }
};

// 映射类型
type Nullable<T> = {
  [P in keyof T]: T[P] | null;
};

// 部分属性
type PartialResource = Partial<ResourceDetail>;

// 必需属性
type RequiredResource = Required<ResourceDetail>;

// 只读属性
type ReadonlyResource = Readonly<ResourceDetail>;

// 排除某些属性
type ResourceWithoutId = Omit<ResourceDetail, 'id'>;

// 选取某些属性
type ResourceBasicInfo = Pick<ResourceDetail, 'id' | 'name'>;



// 数字枚举
enum ResourceType {
  CPU = 0,
  GPU = 1,
  Memory = 2
}

// 字符串枚举
enum ResourceState {
  Active = 'active',
  Inactive = 'inactive',
  Deleted = 'deleted'
}